package cn.tedu.ivos.base.util;

import java.security.SecureRandom;

/* 密码工具类 用于生成随机密码 */
public class PasswordUtil {
    // 允许的字符串: 大写字母、小写字母、数字,特殊字符
    private static final String ALLOWED_CHAR =
            "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*";

    private static final SecureRandom random = new SecureRandom();

    public static String generatePassword(int length) {
        if(length < 5) throw new RuntimeException("密码长度不能小于5!");
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(ALLOWED_CHAR.length());
            char c = ALLOWED_CHAR.charAt(index);
            sb.append(c);
        }
        return sb.toString();
    }

}
